﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    this->setAttribute(Qt::WA_DeleteOnClose);
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    ui->setupUi(this);
    rsql = new Readersql;
    qDebug()<<"//*********au******//";
    qDebug()<<global::AUthority<<global::user;
    m_login=new Login(parent);
    m_login->show();
    m_register=new Register(m_login);
    m_register->show();
    auto f = [&](){
          this->show();
        if(global::AUthority==true){
            qDebug()<<"//***********//";
            qDebug()<<global::AUthority<<global::user;

        }
        else{
            ui->btn_reader->hide();
            ui->book_add_sys->hide();
            ui->emailBtn->hide();
            qDebug()<<"//***********//";
            qDebug()<<global::AUthority<<global::user;
        }
        this->m_login->close();
        this->m_register->close();
       // delete m_register;

    };
    connect(m_login,&Login::sendLoginSuccess,this,f);
    connect(m_login,&Login::sendR,this,[=](){
        m_register->sildeIn();
    });



   connect(ui->emailBtn,&QToolButton::clicked,this,[=](){
        if(!email){
          email=new EmailWIndow(ui->displayWidget);
          email->show();
          ui->title->setText(QString("邮 箱 界 面"));

          ui->emailBtn->setIcon(QIcon(":/images/mail (3).png"));
          ui->emailBtn->setIconSize(QSize(40,40));
          ui->emailBtn->setStyleSheet("border-style:none;font: 290 12pt;color: #3b8ffe; background-color:#031529;");

        }
          if(borrow){
              qDebug()<<"borrow delete";
              borrow->close();
              delete borrow;
              borrow=nullptr;
              ui->btn_return_borrow->setIcon(QIcon(":/images/swap.png"));
              ui->btn_return_borrow->setIconSize(QSize(40,40));
              ui->btn_return_borrow->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));


          }
          if(bok_add){
               qDebug()<<"bok_add delete";
              bok_add->close();
              delete bok_add;
              bok_add=nullptr;
              ui->book_add_sys->setIcon(QIcon(":/images/queue.png"));
              ui->book_add_sys->setIconSize(QSize(40,40));
              ui->book_add_sys->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
          }

          if(book){
               qDebug()<<"book delete";
              book->close();
              delete book;
              book=nullptr;
              ui->book_all->setIcon(QIcon(":/images/search.png"));
              ui->book_all->setIconSize(QSize(40,40));
              ui->book_all->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
          }
          if(m_reader){
               qDebug()<<"book delete";
              m_reader->close();
              delete m_reader;
              m_reader=nullptr;
              ui->btn_reader->setIcon(QIcon(":/images/user.png"));
              ui->btn_reader->setIconSize(QSize(40,40));
              ui->btn_reader->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
          }
          if(privateCenter){
              qDebug()<<"center delete";
              privateCenter->close();
              delete privateCenter;
              privateCenter=nullptr;
              ui->center->setIcon(QIcon(":/images/home.png"));
              ui->center->setIconSize(QSize(40,40));
              ui->center->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
          }

   });

   connect(ui->center,&QToolButton::clicked,this,[=](){
        if(!privateCenter){
          privateCenter=new Information(ui->displayWidget);
          privateCenter->show();
          ui->title->setText(QString("个 人 主 页"));
          ui->center->setIcon(QIcon(":/images/home (1).png"));
          ui->center->setIconSize(QSize(40,40));
          ui->center->setStyleSheet("border-style:none;font: 290 12pt;color: #3b8ffe; background-color:#031529;");
          connect(privateCenter,&Information::logoutClick,this,[=](){
              qDebug()<<"logout Click";
              emit restart();
          });
        }
          if(borrow){
              qDebug()<<"borrow delete";
              borrow->close();
              delete borrow;
              borrow=nullptr;
              ui->btn_return_borrow->setIcon(QIcon(":/images/swap.png"));
              ui->btn_return_borrow->setIconSize(QSize(40,40));
              ui->btn_return_borrow->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));

          }
          if(bok_add){
               qDebug()<<"bok_add delete";
              bok_add->close();
              delete bok_add;
              bok_add=nullptr;
              ui->book_add_sys->setIcon(QIcon(":/images/queue.png"));
              ui->book_add_sys->setIconSize(QSize(40,40));
              ui->book_add_sys->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
          }

          if(book){
               qDebug()<<"book delete";
              book->close();
              delete book;
              book=nullptr;
              ui->book_all->setIcon(QIcon(":/images/search.png"));
              ui->book_all->setIconSize(QSize(40,40));
              ui->book_all->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
          }
          if(m_reader){
               qDebug()<<"book delete";
              m_reader->close();
              delete m_reader;
              m_reader=nullptr;
              ui->btn_reader->setIcon(QIcon(":/images/user.png"));
              ui->btn_reader->setIconSize(QSize(40,40));
              ui->btn_reader->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
          }
          if(email){
              qDebug()<<"email delete";
              email->close();
              delete email;
              email=nullptr;
              ui->emailBtn->setIcon(QIcon(":/images/mail (1).png"));
              ui->emailBtn->setIconSize(QSize(40,40));
              ui->emailBtn->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
          }

   });



//    if(privateCenter){


//    }
     //ui->book_add_sys->click();
     //

   ui->book_all->click();

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_btn_reader_clicked()
{
        if(!m_reader){
            qDebug()<<global::AUthority<<global::user;
            if(global::AUthority==true){
                m_reader=new Reader(ui->displayWidget);
                m_reader->show();
                ui->title->setText(QString("管 理 读 者"));
                ui->btn_reader->setIcon(QIcon(":/images/user (1).png"));
                ui->btn_reader->setIconSize(QSize(40,40));
                ui->btn_reader->setStyleSheet("border-style:none;font: 290 12pt;color: #3b8ffe; background-color:#031529;");

//                ui->btn_reader->hide();
//                ui->book_add_sys->hide();
//                ui->emailBtn->hide();


                if(borrow){
                    qDebug()<<"borrow delete";
                    borrow->close();
                    delete borrow;
                    borrow=nullptr;
                    ui->btn_return_borrow->setIcon(QIcon(":/images/swap.png"));
                    ui->btn_return_borrow->setIconSize(QSize(40,40));
                    ui->btn_return_borrow->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
                    }
                if(bok_add){
                     qDebug()<<"bok_add delete";
                    bok_add->close();
                    delete bok_add;
                    bok_add=nullptr;
                    ui->book_add_sys->setIcon(QIcon(":/images/queue.png"));
                    ui->book_add_sys->setIconSize(QSize(40,40));
                    ui->book_add_sys->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
                    }
                if(email){
                    qDebug()<<"email delete";
                    email->close();
                    delete email;
                    email=nullptr;
                    ui->emailBtn->setIcon(QIcon(":/images/mail (1).png"));
                    ui->emailBtn->setIconSize(QSize(40,40));
                    ui->emailBtn->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
                    }
                if(book){
                     qDebug()<<"book delete";
                    book->close();
                    delete book;
                    book=nullptr;
                    ui->book_all->setIcon(QIcon(":/images/search.png"));
                    ui->book_all->setIconSize(QSize(40,40));
                    ui->book_all->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
                    }
                if(privateCenter){
                    qDebug()<<"center delete";
                    privateCenter->close();
                    delete privateCenter;
                    privateCenter=nullptr;
                    ui->center->setIcon(QIcon(":/images/home.png"));
                    ui->center->setIconSize(QSize(40,40));
                    ui->center->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
                }

            }
            else{QMessageBox::information(this,tr("提示对话框"),tr("没有管理者权限"), QMessageBox::Ok);}//return;}
        }


        //ui->btn_reader->setIcon(QIcon(":/images/search (1)"))





}


void MainWindow::on_pushButton_3_clicked()
{
    exit(0);
}


void MainWindow::on_book_all_clicked()
{
        if(!book){
        qDebug()<<global::AUthority<<global::user;
        book=new all_book(ui->displayWidget);
        qDebug()<<"entering book";
        book->show();
        ui->title->setText(QString("查 询 图 书"));
        ui->book_all->setIcon(QIcon(":/images/search (1).png"));
        ui->book_all->setIconSize(QSize(40,40));
        ui->book_all->setStyleSheet("border-style:none;font: 290 12pt;color: #3b8ffe; background-color:#031529;");
        }
        if(borrow){
            qDebug()<<"borrow delete";
            borrow->close();
            delete borrow;
            borrow=nullptr;
            ui->btn_return_borrow->setIcon(QIcon(":/images/swap.png"));
            ui->btn_return_borrow->setIconSize(QSize(40,40));
            ui->btn_return_borrow->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
            }
        if(bok_add){
             qDebug()<<"bok_add delete";
            bok_add->close();
            delete bok_add;
            bok_add=nullptr;
            ui->book_add_sys->setIcon(QIcon(":/images/queue.png"));
            ui->book_add_sys->setIconSize(QSize(40,40));
            ui->book_add_sys->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
        }
        if(email){
            qDebug()<<"email delete";
            email->close();
            delete email;
            email=nullptr;
            ui->emailBtn->setIcon(QIcon(":/images/mail (1).png"));
            ui->emailBtn->setIconSize(QSize(40,40));
            ui->emailBtn->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
        }
        if(m_reader){
             qDebug()<<"book delete";
            m_reader->close();
            delete m_reader;
            m_reader=nullptr;
            ui->btn_reader->setIcon(QIcon(":/images/user.png"));
            ui->btn_reader->setIconSize(QSize(40,40));
            ui->btn_reader->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
        }
        if(privateCenter){
            qDebug()<<"center delete";
            privateCenter->close();
            delete privateCenter;
            privateCenter=nullptr;
            ui->center->setIcon(QIcon(":/images/home.png"));
            ui->center->setIconSize(QSize(40,40));
            ui->center->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
        }


}


void MainWindow::on_btn_return_borrow_clicked()
{
    if(!borrow){
    qDebug()<<global::AUthority<<global::user;
    borrow=new Borrow_book(ui->displayWidget);
     qDebug()<<"entering borrow_system";
     borrow->show();
     ui->title->setText(QString("借 阅 归 还"));
     ui->btn_return_borrow->setIcon(QIcon(":/images/swap (1).png"));
     ui->btn_return_borrow->setIconSize(QSize(40,40));
     ui->btn_return_borrow->setStyleSheet("border-style:none;font: 290 12pt;color: #3b8ffe; background-color:#031529;");
}
     if(m_reader){
          qDebug()<<"book delete";
         m_reader->close();
         delete m_reader;
         m_reader=nullptr;
         ui->btn_reader->setIcon(QIcon(":/images/user.png"));
         ui->btn_reader->setIconSize(QSize(40,40));
         ui->btn_reader->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
     }
     if(bok_add){
          qDebug()<<"bok_add delete";
         bok_add->close();
         delete bok_add;
         bok_add=nullptr;
         ui->book_add_sys->setIcon(QIcon(":/images/queue.png"));
         ui->book_add_sys->setIconSize(QSize(40,40));
         ui->book_add_sys->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
     }
     if(email){
         qDebug()<<"email delete";
         email->close();
         delete email;
         email=nullptr;
         ui->emailBtn->setIcon(QIcon(":/images/mail (1).png"));
         ui->emailBtn->setIconSize(QSize(40,40));
         ui->emailBtn->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
     }
     if(book){
          qDebug()<<"book delete";
         book->close();
         delete book;
         book=nullptr;
         ui->book_all->setIcon(QIcon(":/images/search.png"));
         ui->book_all->setIconSize(QSize(40,40));
         ui->book_all->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
     }
     if(privateCenter){
         qDebug()<<"center delete";
         privateCenter->close();
         delete privateCenter;
         privateCenter=nullptr;
         ui->center->setIcon(QIcon(":/images/home.png"));
         ui->center->setIconSize(QSize(40,40));
         ui->center->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
     }

}


void MainWindow::on_book_add_sys_clicked()
{
    if(!bok_add){
    bok_add=new single_book_add(ui->displayWidget);
    qDebug()<<"entering adding system";
    bok_add->show();
    ui->title->setText(QString("添 加 图 书"));
    ui->book_add_sys->setIcon(QIcon(":/images/queue (1).png"));
    ui->book_add_sys->setIconSize(QSize(40,40));
    ui->book_add_sys->setStyleSheet("border-style:none;font: 290 12pt;color: #3b8ffe; background-color:#031529;");

    }
    if(borrow){
        qDebug()<<"borrow delete";
        borrow->close();
        delete borrow;
        borrow=nullptr;
        ui->btn_return_borrow->setIcon(QIcon(":/images/swap.png"));
        ui->btn_return_borrow->setIconSize(QSize(40,40));
        ui->btn_return_borrow->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
        }
    if(book){
         qDebug()<<"book delete";
        book->close();
        delete book;
        book=nullptr;
        ui->book_all->setIcon(QIcon(":/images/search.png"));
        ui->book_all->setIconSize(QSize(40,40));
        ui->book_all->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));

    }
    if(email){
        qDebug()<<"email delete";
        email->close();
        delete email;
        email=nullptr;
        ui->emailBtn->setIcon(QIcon(":/images/mail (1).png"));
        ui->emailBtn->setIconSize(QSize(40,40));
        ui->emailBtn->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
    }
    if(m_reader){
         qDebug()<<"book delete";
        m_reader->close();
        delete m_reader;
        m_reader=nullptr;
        ui->btn_reader->setIcon(QIcon(":/images/user.png"));
        ui->btn_reader->setIconSize(QSize(40,40));
        ui->btn_reader->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
    }
    if(privateCenter){
        qDebug()<<"center delete";
        privateCenter->close();
        delete privateCenter;
        privateCenter=nullptr;
        ui->center->setIcon(QIcon(":/images/home.png"));
        ui->center->setIconSize(QSize(40,40));
        ui->center->setStyleSheet(QString("QToolButton{border-style:none;font: 290 12pt ;color: rgb(255, 255, 255); background-color:#031529;}QToolButton:hover{font: 290 14pt ;color:#ffffff;}QToolButton:pressed{font: 290 14pt ;color:#ffffff;}"));
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent*event)
{
    if (event->buttons() & Qt::LeftButton&&event->pos().x()>80&&event->pos().x()<800&&event->pos().y()>0&&event->pos().y()<66)
    {
        QRect desktopRc =  QGuiApplication::primaryScreen()->geometry();
        QPoint curPoint = event->globalPos() - m_dragPos;
        if (event->globalY() > desktopRc.height())
        {
            curPoint.setY(desktopRc.height() - m_dragPos.y());
        }
        move(curPoint);
    }
    QWidget::mouseMoveEvent(event);
}

void MainWindow::mousePressEvent(QMouseEvent*event)
{
    if (event->button() == Qt::LeftButton)
    {
        if(event->pos().x()>80&&event->pos().x()<800&&event->pos().y()>0&&event->pos().y()<66)
            m_dragPos = QCursor::pos() - frameGeometry().topLeft();
    }
    QWidget::mousePressEvent(event);
}


void MainWindow::on_minBtn_clicked()
{
    showMinimized();
}

